﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Navigant.Application;
using Navigant.Infrastructure;
using Navigant.Infrastructure.Cases;
using Navigant.Infrastructure.DomainBase;
using Navigant.Model.Cases;
using Navigant.Model.Custodians;
using Navigant.Model.PreservationNotices;

namespace Navigant.UnitTests
{
    
    
    /// <summary>
    ///This is a test class for PreservationNoticeSendingServiceTest and is intended
    ///to contain all PreservationNoticeSendingServiceTest Unit Tests
    ///</summary>
    [TestClass()]
    public class PreservationNoticeSendingServiceTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for SendNotices
        ///</summary>
        [TestMethod()]
        public void SendNoticesTestToMultipleCustodians()
        {
            ICase currentCase = CaseService.GetCaseBy(new Uri(ConfigurationManager.AppSettings["testCaseSite"]));
            CustodianService service = new CustodianService(currentCase);
            IList<Custodian> custodians = service.FindAll();
            PreservationNoticeTemplateService templateService = new PreservationNoticeTemplateService(currentCase);
            int preservationNoticeTemplateId = templateService.FindAll()[0].Id;
            string fromAddress = "test@test.com";
            PreservationNoticeSendingService.SendNotices(currentCase, custodians, preservationNoticeTemplateId, fromAddress);
        }

        /// <summary>
        ///A test for SendNotice
        ///</summary>
        [TestMethod()]
        public void SendNoticeTestToOneCustodian()
        {
            ICase currentCase = CaseService.GetCaseBy(new Uri(ConfigurationManager.AppSettings["testCaseSite"]));
            CustodianService service = new CustodianService(currentCase);
            Custodian custodian = service.FindAll()[0];
            PreservationNoticeTemplateService templateService = new PreservationNoticeTemplateService(currentCase);
            int preservationNoticeTemplateId = templateService.FindAll()[0].Id;
            string fromAddress = "test@test.com";
            PreservationNoticeSendingService.SendNotice(currentCase, custodian, preservationNoticeTemplateId, fromAddress);
        }

        /// <summary>
        ///A test for SendNotice
        ///</summary>
        [DeploymentItem("TestAttachment.pdf"), TestMethod()]
        public void SendNoticeTestWithAttachmentToOneCustodian()
        {
            ICase currentCase = CaseService.GetCaseBy(new Uri(ConfigurationManager.AppSettings["testCaseSite"]));
            CustodianService service = new CustodianService(currentCase);
            Custodian custodian = service.FindAll()[0];
            PreservationNoticeTemplateService templateService = new PreservationNoticeTemplateService(currentCase);
            int preservationNoticeTemplateId = this.SaveNewPreservationNoticeTemplateWithAttachment().Id;
            string fromAddress = "test@test.com";
            PreservationNoticeSendingService.SendNotice(currentCase, custodian, preservationNoticeTemplateId, fromAddress);
        }

        private PreservationNoticeTemplate SaveNewPreservationNoticeTemplateWithAttachment()
        {
            IUnitOfWork unitOfWork = new UnitOfWork();
            PreservationNoticeTemplateService service = this.GetPreservationNoticeTemplateService(unitOfWork);
            PreservationNoticeTemplate template = new PreservationNoticeTemplate(CaseService.GetCaseBy(new Uri(ConfigurationManager.AppSettings["testCaseSite"])), 
                "Test Template", 
                "This is a test template", "[Image|hi.jpg] </br> The message body for test template.  [URL] ");
            string attachmentFileName = "TestAttachment.pdf";
            byte[] content = null;
            using (Stream stream = File.Open(attachmentFileName, FileMode.Open))
            {
                content = new byte[stream.Length];
                stream.Read(content, 0, (int)stream.Length);
            }
            template.AddAttachment(new Attachment(content, attachmentFileName));
            service.SavePreservationNoticeTemplate(template);
            unitOfWork.Commit();
            return template;
        }

        private PreservationNoticeTemplateService GetPreservationNoticeTemplateService(IUnitOfWork unitOfWork)
        {
            Case currentCase = CaseService.GetCaseBy(new Uri(ConfigurationManager.AppSettings["testCaseSite"]));
            return new PreservationNoticeTemplateService(currentCase, unitOfWork);
        }

        /// <summary>
        ///A test for SendNotice
        ///</summary>
        [TestMethod()]
        public void SendNoticeTestToOneCustodianById()
        {
            ICase currentCase = CaseService.GetCaseBy(new Uri(ConfigurationManager.AppSettings["testCaseSite"]));
            CustodianService service = new CustodianService(currentCase);
            int custodianId = service.FindAll()[0].Id;
            PreservationNoticeTemplateService templateService = new PreservationNoticeTemplateService(currentCase);
            int preservationNoticeTemplateId = templateService.FindAll()[0].Id;
            string fromAddress = "test@test.com";
            PreservationNoticeSendingService.SendNotice(currentCase, custodianId, preservationNoticeTemplateId, fromAddress);
        }
    }
}
